package org.itboys.mobile.service.mongo.mobile.collection;

import com.google.common.collect.Maps;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mdl.entity.mongo.collection.MdlCollection;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.mobile.member.SundayMobileMemberService;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/12/1
 * 描述：迈迪龙_手机端_收藏
 */
@Service
public class MdlMobileCollectionService extends BaseMongoService<MdlCollection>{

    @Autowired
    private SundayMobileProductService productService;
    @Autowired
    private SundayMobileMemberService memberService;
    @Resource(name = "mdlDs")
    private MongoDataSource mdlDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mdlDataSource;
    }

    @Override
    protected Class<MdlCollection> getEntityClass() {
        return MdlCollection.class;
    }

    /**
     * 判断用户是否收藏。 1商品，2待定
     * @param memberId
     * @param type
     * @return
     */
    public Integer checkIsCollection(Long memberId,Integer type,Long otherId){

        Map<String,Object> param=new HashMap<String,Object>();
        param.put("memberId",memberId);
        param.put("type",type);
        param.put("otherId",otherId);
        List<MdlCollection> collections=super.list(param);
        if(ListUtil.isNotNull(collections)){
            return 1;
        }
        return 0;
    }

    /**
     * 新增收藏  或者取消收藏
     * @param memberId
     * @param type
     * @param otherId
     */
    public void collection(Long memberId,Integer type,Long otherId){
        Map<String, Object> param = Maps.newHashMapWithExpectedSize(4);
        param.clear();
        param.put("memberId",memberId);
        param.put("type",type);
        param.put("otherId",otherId);

        List<MdlCollection> collections=super.list(param);
        if(ListUtil.isNull(collections)){
            super.save(new MdlCollection(memberId,type,otherId));
        }else {

            super.doDelete(param);
        }
    }


    /**
     * 分页查询收藏列表  包括收藏的商品
     * @param request
     * @param memberId
     * @param type
     * @return
     */
    public List<Map<String,Object>> selectCollection(HttpServletRequest request, Long memberId, Integer type){
        List<Map<String,Object>> results=new ArrayList<Map<String,Object>>();

        Map<String ,Object> param= new HashMap<String,Object>();
        List<Long> otherIds=new ArrayList<Long>();
        param.put("memberId",memberId);
        param.put("type",type);
        List<MdlCollection> collections=super.list(param);
        if(ListUtil.isNull(collections))return results;
        //查询收藏商品
        if(type==1){
            param.clear();
            param = ParamUtil.packageMysqlPageParam(request,"name");
            param.put("ids",otherIds);
            List<SundayProduct> products=productService.select(param);
            //获取商品真实价格
            //products=productService.getRealPrice2(products,memberId,null,null);
            if(ListUtil.isNotNull(products)){
                for(SundayProduct product:products){
                    Map<String,Object> result = new HashMap<String,Object>();
                    result.put("id",product.getId());
                    result.put("name",product.getName());
                    result.put("viceName",product.getViceName());
                    result.put("image",product.getImage());
                    result.put("saleNumber",product.getSaleNumber());
                    result.put("price",product.getPrice());
                    result.put("marketPrice",product.getMarketPrice());
                    result.put("collections",0);
                    result.put("comments",0);
                    result.put("createTime",product.getCreateTime());
                    result.put("createDate",product.getCreateDate());
                    results.add(result);
                }

            }
        }
        return results;
    }



}
